package com.qingke.project_management.command_impl;


import org.apache.ibatis.session.SqlSession;

import com.qingke.project_management.App;
import com.qingke.project_management.Console;
import com.qingke.project_management.IDao.MaterialMapper;
import com.qingke.project_management.IDao.UnitMapper;
import com.qingke.project_management.command.Command;
import com.qingke.project_management.pojo.Material;
import com.qingke.project_management.pojo.Unit;

public class NewCommand extends Command {

public void execute() {
		
		if(App.getInstance().getUser()==null){
			Console.println("您还未登录，没有权限执行该操作");
			return;
		}
		
		
		boolean doit = true;
		while(doit){
			Console.println("请输入要创建的内容:");
			Console.println("【1】创建新材料（实现）");
			Console.println("【2】创建新项目");
			Console.println("【3】创建新采购计划");
			Console.println("【4】创建新领用单");
			Console.println("【5】创建新文档");
			int choice = Console.askInputInt("要查看的选项");
			switch (choice) {
			case 1:
				newMaterial();
				doit = false;
				break;
			case 2:
				newProject();
				doit = false;
				break;
			case 3:
				newPurchasePlan();
				doit = false;
				break;
			case 4:
				newRequisition();
				doit = false;
				break;
			case 5:
				newDocument();
				doit = false;
				break;
			default:
				Console.println("命令不正确");
				break;
			}
		}
	}
	
	private void newDocument() {
		// TODO Auto-generated method stub
		System.out.println("这是一个付费功能,充值才能变更强");
	}
	
	private void newRequisition() {
		// TODO Auto-generated method stub
		System.out.println("这是一个付费功能,充值才能变更强");
	}
	
	private void newPurchasePlan() {
		// TODO Auto-generated method stub
		System.out.println("这是一个付费功能,充值才能变更强");
	}
	
	private void newProject() {
		// TODO Auto-generated method stub
		System.out.println("这是一个付费功能,充值才能变更强");
	}
	
	private void newMaterial() {
		// TODO Auto-generated method stub
		SqlSession session = getSession();
		Material material = new Material();
		String name = Console.askInput("材料名");
		String model = Console.askInput("请输入型号");
		String unitName = Console.askInput("计量单位");
		Unit unit = getUnit(unitName,session);
		material.setCode(" ");
		material.setModel(model);
		material.setName(name);
		material.setUnitId(unit.getId());
		MaterialMapper materialMapper = session.getMapper(MaterialMapper.class);
		materialMapper.insert(material);
		session.commit();
//		material = materialMapper.selectByName(name);
	}

	private Unit getUnit(String name,SqlSession session) {
		
		UnitMapper unitMapper = session.getMapper(UnitMapper.class);
		Unit unit = unitMapper.selectByName(name);
		if(unit==null){
			unit = new Unit();
			unit.setCode(" ");
			unit.setDescription(" ");
			unit.setName(name);
			unitMapper.insert(unit);
			unit = unitMapper.selectByName(name);
		}
		return unit;
	}

}
